<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
		"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<title>doh.robot Slider Test</title>

	<style>
		@import "../../../../util/doh/robot/robot.css";
	</style>

	<!-- required: dojo.js -->
	<script type="text/javascript" src="../../../../dojo/dojo.js"
		djConfig="isDebug: true, parseOnLoad: true"></script>

	<script type="text/javascript">
		dojo.require("dijit.dijit"); // optimize: load dijit layer
		dojo.require("dijit.robotx");

		var arrowSliders=['slider1','slider2','programaticSlider'];
		var arrowSliderInit_min=[0.2,10,1200];
		var arrowSliderInit_max=[99.8,90,2800];
		var accessibleSliders=['slider1','slider2','slider3','programaticSlider']
		var accessibleSliderInit_min=[0.2,10,2,1200];
		var accessibleSliderInit_max=[99.8,90,2,2800];
		var sliderIds = ["slider1", "slider2", "slider3", "sliderH2", "programaticSlider"];

		onChange={slider1:'slider1input', slider2:'slider2input', programaticSlider:'sliderProgInput'};

		var boundaryTest=function(slider, initValue, finalValue){
			var d = new doh.Deferred();
			slider.attr('value', initValue);
			var button = slider._descending?(initValue<finalValue?slider.decrementButton:slider.incrementButton):(initValue<finalValue?slider.incrementButton:slider.decrementButton);
			doh.robot.sequence(function(){
				dijit.scrollIntoView(slider.domNode);
			},500);
			doh.robot.mouseMoveAt(button, 500);
			doh.robot.mouseClick({left:true}, 500);
			doh.robot.sequence(d.getTestErrback(function(){
				doh.is(finalValue, new Number(slider.attr('value')).toFixed(1));
				if(onChange[slider.id]){
					doh.is(finalValue, parseFloat(dojo.byId(onChange[slider.id]).value));
				}
				doh.robot.mouseMoveAt(button, 500);
				doh.robot.mouseClick({left:true}, 500);
				doh.robot.sequence(d.getTestCallback(function(){
					doh.is(finalValue, new Number(slider.attr('value')).toFixed(1));
					if(onChange[slider.id]){
						doh.is(finalValue, parseFloat(dojo.byId(onChange[slider.id]).value));
					}
				}), 500);
			}), 500);
			return d;
		};

		var buttonHeldTest=function(slider, initValue, finalValue){
			var d = new doh.Deferred();
			slider.attr('value', initValue);
			var button = slider._descending?(initValue<finalValue?slider.decrementButton:slider.incrementButton):(initValue<finalValue?slider.incrementButton:slider.decrementButton);
			doh.robot.sequence(function(){
				dijit.scrollIntoView(slider.domNode);
			},500);
			doh.robot.mouseMoveAt(button, 500);
			doh.robot.mousePress({left:true}, 500);
			doh.robot.mouseRelease({left:true}, 5000);
			doh.robot.sequence(d.getTestCallback(function(){
				doh.is(finalValue, new Number(slider.attr('value')).toFixed(1));
				if(onChange[slider.id]){
					doh.is(finalValue, parseFloat(dojo.byId(onChange[slider.id]).value));
				}
			}), 500);
			return d;
		};

		var mouseWheelTest=function(slider, initValue, finalValue){
			if(slider.discreteValues != Infinity){
				var d = new doh.Deferred();
				slider.attr('value', initValue);
				var direction=(finalValue-initValue)/Math.abs(finalValue-initValue);
				var delta = Math.ceil((slider.discreteValues-1)*Math.abs(doh.robot.mouseWheelSize)/doh.robot.mouseWheelSize);
				doh.robot.sequence(function(){
					dijit.scrollIntoView(slider.domNode);
				},500);
				doh.robot.mouseMoveAt(slider.focusNode, 500);
				doh.robot.mouseClick({left:true}, 500);
				doh.robot.mouseWheel(delta*direction, 500, Math.abs(delta)*100);
				doh.robot.sequence(d.getTestCallback(function(){
					doh.is(finalValue, new Number(slider.attr('value')).toFixed(1));
					if(onChange[slider.id]){
						doh.is(finalValue, parseFloat(dojo.byId(onChange[slider.id]).value));
					}
				}), 500);
				return d;
			}
		};
		
		dojo.addOnLoad(function(){
			doh.robot.initRobot('../test_Slider.html');

			// Test initial values
			doh.register("setUp",{
				name:"setUp",
				runTest:function(){
					doh.is(10, dijit.byId("slider1").attr("value"), "horizontal1");
					doh.is(10, dijit.byId("slider2").attr("value"), "vertical1");
					doh.is(2, dijit.byId("slider3").attr("value"), "horizontal2");
					doh.is(10, dijit.byId("sliderH2").attr("value"), "sliderH2");
					doh.is(1400, dijit.byId("programaticSlider").attr("value"), "programaticSlider");
				}
			});

			doh.register("setvaluetest",{
				name:"valid",
				slider:arrowSliders[0],
				runTest:function(){
					this.slider=dijit.byId(this.slider);
					this.slider.attr('value', 50);
					doh.is(50, parseFloat(dojo.byId(onChange[this.slider.id]).value));
				}
			});

			doh.register("setvaluetest",{
				name:"null",
				slider:arrowSliders[0],
				runTest:function(){
					this.slider=dijit.byId(this.slider);
					this.slider.attr('value', null);
					doh.is(0, parseFloat(dojo.byId(onChange[this.slider.id]).value));
				}
			});

			doh.register("disabledTest",{
				name:arrowSliders[0]+'_setUp',
				slider:arrowSliders[0],
				timeout:30000,
				runTest:function(){
					var d = new doh.Deferred();
					doh.robot.mouseMoveAt('disableButton',500);
					doh.robot.mouseClick({left:true}, 500);
					doh.robot.sequence(function(){ d.callback(true); },500);
					return d;
				}
			});

			doh.register("disabledTest",{
				name:arrowSliders[0]+'_min',
				slider:arrowSliders[0],
				value:arrowSliderInit_min[0],
				timeout:30000,
				runTest:function(){
					this.slider=dijit.byId(this.slider);
					return boundaryTest(this.slider, this.value, this.value);
				}
			});

			doh.register("disabledTest",{
				name:arrowSliders[0]+'_tearDown',
				slider:arrowSliders[0],
				timeout:30000,
				runTest:function(){
					var d = new doh.Deferred();
					doh.robot.mouseMoveAt('enableButton',500);
					doh.robot.mouseClick({left:true}, 500);
					doh.robot.sequence(function(){ d.callback(true); },500);
					return d;
				}
			});

			for(var i=0; i<arrowSliders.length; i++){
				doh.register("buttonBoundaryTest",{
					name:arrowSliders[i]+'_min',
					slider:arrowSliders[i],
					value:arrowSliderInit_min[i],
					timeout:30000,
					runTest:function(){
						this.slider=dijit.byId(this.slider);
						return boundaryTest(this.slider, this.value, this.slider.minimum);
					}
				});
				doh.register("buttonBoundaryTest",{
					name:arrowSliders[i]+'_max',
					slider:arrowSliders[i],
					value:arrowSliderInit_max[i],
					timeout:30000,
					runTest:function(){
						this.slider=dijit.byId(this.slider);
						return boundaryTest(this.slider, this.value, this.slider.maximum);
					}
				});
			}

			for(var i=1; i<arrowSliders.length; i++){
				doh.register("buttonHeldTest",{
					name:arrowSliders[i]+'_minToMax',
					slider:arrowSliders[i],
					timeout:30000,
					runTest:function(){
						this.slider=dijit.byId(this.slider);
						return buttonHeldTest(this.slider, this.slider.minimum, this.slider.maximum);
					}
				});
				doh.register("buttonHeldTest",{
					name:arrowSliders[i]+'_maxToMin',
					slider:arrowSliders[i],
					timeout:30000,
					runTest:function(){
						this.slider=dijit.byId(this.slider);
						return buttonHeldTest(this.slider, this.slider.maximum, this.slider.minimum);
					}
				});
			}


			for(var i=1; i<accessibleSliders.length; i++){
				doh.register("mouseWheelTest",{
					name:accessibleSliders[i]+'_minToMax',
					slider:accessibleSliders[i],
					timeout:30000,
					runTest:function(){
						this.slider=dijit.byId(this.slider);
						return mouseWheelTest(this.slider, this.slider.minimum, this.slider.maximum);
					}
				});
				doh.register("mouseWheelTest",{
					name:accessibleSliders[i]+'_maxToMin',
					slider:accessibleSliders[i],
					timeout:30000,
					runTest:function(){
						this.slider=dijit.byId(this.slider);
						return mouseWheelTest(this.slider, this.slider.maximum, this.slider.minimum);
					}
				});
			}

			doh.register("dijit.form.Slider",
				{
					name:"slider1",
					setUp:function(){
						dijit.byId("slider1").attr('value',10);
					},
					timeout:4000, // this is the animated slider so there is a 500ms delay
					runTest:function(){
						var d = new doh.Deferred();
						var slider=dijit.byId("slider1");
						dijit.scrollIntoView(slider.domNode);
						doh.robot.mouseMoveAt(slider.focusNode, 500);
						doh.robot.mousePress({left:true}, 500);
						// drag to 20% marker
						doh.robot.mouseMoveAt(dojo.query("div[style*='20%']", dojo.byId('dijit_form_HorizontalRule_0'))[0], 500);
						doh.robot.mouseRelease({left:true}, 500);
						doh.robot.sequence(function(){
							var value=slider.attr('value');
							if(value>=19&&value<=21){
								d.callback(true);
							}else{
								d.errback("slider1 failed. Expected ~20, got "+value);
							}
						}, 1400);
						return d;
					},
					tearDown:function(){
						dijit.byId("slider1").attr('value',20);
					}
				}
			);

			doh.register("dijit.form.Slider",
				{
					name:"slider2",
					timeout:3500,
					setUp:function(){
						dijit.byId("slider2").attr('value',10);
					},
					runTest:function(){
						var d = new doh.Deferred();
						var slider=dijit.byId("slider2");
						dijit.scrollIntoView(slider.domNode);
						doh.robot.mouseMoveAt(slider.focusNode, 500);
						doh.robot.mousePress({left:true}, 500);
						// drag to 20% marker (Slider is in descending order so it's 100-20=80%)
						var marker=dojo.query("div[style*='80%']", dojo.byId('dijit_form_VerticalRule_1'))[0];
						doh.robot.mouseMoveAt(marker, 500, 100, 0, 0);
						doh.robot.mouseRelease({left:true}, 500);
						doh.robot.sequence(function(){
							var value=slider.attr('value');
							if(value==20){
								d.callback(true);
							}else{
								d.errback("slider2 failed. Expected 20, got "+value);
							}
						}, 900);
						return d;
					}
				}
			);

			doh.register("dijit.form.Slider",
				{
					name:"slider3",
					timeout:3500,
					setUp:function(){
						dijit.byId("programaticSlider").attr('value',1000);
					},
					runTest:function(){
						var d = new doh.Deferred();
						var slider=dijit.byId("programaticSlider");
						dijit.scrollIntoView(slider.domNode);
						doh.robot.mouseMoveAt(slider.focusNode, 500);
						doh.robot.mousePress({left:true}, 500);
						// drag to the top
						doh.robot.mouseMoveAt(dojo.query("div[style*='0%']", dojo.byId('dijit_form_VerticalRule_2'))[0], 500, 100, 0, 0);
						doh.robot.mouseRelease({left:true}, 500);
						doh.robot.sequence(function(){
							var value=slider.attr('value');
							if(value==3000){
								d.callback(true);
							}else{
								d.errback("slider3 failed. Expected 3000, got "+value);
							}
						}, 900);
						return d;
					}
				}
			);

			doh.run();
		});
	</script>
</head>
</html>
